package com.voltmao.controller.module.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.persistent.mapper.SysMessageMapper;
import com.voltmao.persistent.model.SysMessage;
import com.voltmao.service.application.sys.MessageServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 系统通知，系统消息控制层
 *
 * @author zhanglin
 * @date 2018/4/23
 */
@Controller
@RequestMapping(value = "/module/sys/message")
public class MessageController {
    @Autowired
    MessageServiceImpl messageService;
    @Autowired
    private SysMessageMapper sysMessageMapper;

    @ResponseBody
    @RequestMapping(value = "/gridList.json", method = RequestMethod.POST)
    public GridViewResult getMsgGrid(HttpServletRequest request) {
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/module/sys/message/gridList.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                try {
                    apiResult.status = Constants.SUCCESS.toString();
                    apiResult.msg = Constants.SUCCESS.getDes();
                    return messageService.selectDataPageMsgs(params);
                } catch (Exception e){
                    e.printStackTrace();
                    apiResult.status = Constants.PARAMETER_ERROR.toString();
                    apiResult.msg = Constants.PARAMETER_ERROR.getDes();
                    return null;
                }
            }
            @Override
            public Object defalutForm(HttpServletRequest request) {
                return null;
            }
        });
    }

    @ResponseBody
    @RequestMapping("/apiUpdate.json")
    public ApiResult insertOrUpdate(HttpServletRequest request){
        /*
         * 修改每条记录的读状态
         */
        return new DTOSmith(request).builderApiResult((request1, apiResult) -> {
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            SysMessage sm = sysMessageMapper.selectByPrimaryKey(params.get("dataId")+"");
            System.out.println(params.toString());
            try {
                messageService.insertOrUpdateMsg(sm);
                apiResult.setStatus(Constants.SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                apiResult.setStatus(Constants.UPDATE_FAIVAL);
            }
            return null;
        });
    }

    @ResponseBody
    @RequestMapping("/apiCounts.json")
    public ApiResult countsUnRead(HttpServletRequest request){
        /*
         * 查询未读状态的数量
         */
        return new DTOSmith(request).builderApiResult((request1, apiResult) -> {
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            System.out.println(params.toString());
            try {
                int counts = messageService.countMsgUnRead(params.get("userId")+"");
                apiResult.setStatus(Constants.SUCCESS);
                return counts;
            } catch (Exception e) {
                e.printStackTrace();
                apiResult.setStatus(Constants.UPDATE_FAIVAL);
                return null;
            }
        });
    }
}
